package com.spic.business.train.autonomic.controller;

import com.spic.business.train.autonomic.domain.TrainAutonomicLearning;
import com.spic.business.train.autonomic.service.ITrainAutonomicLearningService;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 课件自主学习Controller
 *
 * @author spic
 * @date 2021-10-21
 */
@RestController
@RequestMapping("/train/autonomic")
public class TrainAutonomicLearningController extends BaseController
{
    @Autowired
    private ITrainAutonomicLearningService trainAutonomicLearningService;

    /**
     * 查询课件自主学习列表
     */
    @PreAuthorize(hasPermi = "business/train:autonomic:list")
    @GetMapping("/list")
    public TableDataInfo list(TrainAutonomicLearning trainAutonomicLearning)
    {
        startPage();
        List<TrainAutonomicLearning> list = trainAutonomicLearningService.selectTrainAutonomicLearningList(trainAutonomicLearning);
        return getDataTable(list);
    }

    /**
     * 导出课件自主学习列表
     */
    @PreAuthorize(hasPermi = "business/train:autonomic:export")
    @Log(title = "课件自主学习", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainAutonomicLearning trainAutonomicLearning) throws IOException
    {
        List<TrainAutonomicLearning> list = trainAutonomicLearningService.selectTrainAutonomicLearningList(trainAutonomicLearning);
        ExcelUtil<TrainAutonomicLearning> util = new ExcelUtil<TrainAutonomicLearning>(TrainAutonomicLearning.class);
        util.exportExcel(response, list, "课件自主学习数据");
    }

    /**
     * 获取课件自主学习详细信息
     */
    @PreAuthorize(hasPermi = "business/train:autonomic:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(trainAutonomicLearningService.selectTrainAutonomicLearningById(id));
    }

    /**
     * 修改课件自主学习
     */
//    @PreAuthorize(hasPermi = "business/train:autonomic:edit") //注释掉 因为课件学习时需要更新进度
    @Log(title = "课件自主学习", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainAutonomicLearning trainAutonomicLearning)
    {

        TrainAutonomicLearning item= trainAutonomicLearningService.selectTrainAutonomicLearningById(trainAutonomicLearning.getId());


        //实际学习时间大于总时间不更新
        if(item.getRealityTrainTime()>=item.getTrainTime()){
            return toAjax(200);
        }
        //每次加一分钟
        trainAutonomicLearning.setRealityTrainTime(item.getRealityTrainTime()+1);

        if(trainAutonomicLearning.getRealityTrainTime()>item.getTrainTime()){
            trainAutonomicLearning.setRealityTrainTime(item.getTrainTime());
            trainAutonomicLearning.setRealityFileProgress(100);
        }

        int schedule= (int)((double)trainAutonomicLearning.getRealityTrainTime() / item.getTrainTime() * 100);
        trainAutonomicLearning.setSchedule(schedule);
        trainAutonomicLearning.setRealityFileProgress((double)trainAutonomicLearning.getRealityTrainTime() / item.getTrainTime() * 100);

        if(trainAutonomicLearning.getSchedule()>=100){
            trainAutonomicLearning.setRealityTrainTime(item.getTrainTime());
            trainAutonomicLearning.setRealityFileProgress(100);
            trainAutonomicLearning.setSchedule(100);
        }

        if(item.getStartTime() ==null){
            trainAutonomicLearning.setStartTime(DateUtils.getNowDate());
        }
        if(trainAutonomicLearning.getRealityFileProgress()>99){
            trainAutonomicLearning.setEndTime(DateUtils.getNowDate());
        }

        return toAjax(trainAutonomicLearningService.updateProgress(trainAutonomicLearning));
    }
}
